﻿/**********************************************************************
/// <summary>
// Name of File     :   PageMessage.cs
// Author           :   Parminder Singh
// Create date      :   17th Sep 2011
/// </summary>
************************************************************************/
using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
using System.Xml;
//using Crystal.Common.Model; 

namespace Crystal.CLL
{
    /// <summary>
    /// 
    /// This class is used to store all messages. 
    /// </summary>
    public static class MessageManager
    {
        #region Member Variable
        private static Dictionary<string, string> _currentMessage = null;
        #endregion

        #region Constructors
        static MessageManager()
        {
            LoadMessagesFromXml();
        }

        #endregion

        #region Methods
        /// <summary>
        /// This Function is used to Reload Message From XML File it Check that Hashtable object Current is null
        /// then it Reload message from Xml Files
        /// </summary>
        public static void ReLoadMessagesFromXml()
        {
            _currentMessage = null;
            LoadMessagesFromXml();
        }

        /// <summary>
        /// This Function is used to load Message From XML File it Check that Hashtable object Current is null
        /// then it Load message from Xml Files Otherwise it it doesnot Load message
        /// </summary>
        private static void LoadMessagesFromXml()
        {
            //CommonCLL common = new CommonCLL();
            if (_currentMessage == null)
            {
                DataSet dsMessages = new DataSet();
                Assembly _assembly;
                _assembly = Assembly.GetExecutingAssembly();
                dsMessages.ReadXml(_assembly.GetManifestResourceStream("Crystal.CLL.Components.EntityFiles.Messages.xml"));
                //XmlDocument doc=  common.XMLResource("Crystal.CLL\\Components\\EntityFiles\\Messages.xml");
                _currentMessage = new Dictionary<string, string>();

                //dsMessages.ReadXml("file:///D:/Dupinder Singh/Projects/Crystal/Code/clarityir/Crystal/CrystalAdmin.Source/Crystal.CLL/Components/EntityFiles/Messages.xml");
                if (dsMessages.Tables.Count != 0)
                {
                    DataTable dtMessages = dsMessages.Tables[0];
                    if (dtMessages.Rows.Count != 0)
                    {
                        foreach (DataRow drMessage in dtMessages.Rows)
                            _currentMessage.Add(Convert.ToString(drMessage[0]), Convert.ToString(drMessage[1]));
                    }
                    dtMessages = null;
                }
                dsMessages = null;
            }
        }

        /// <summary>
        /// Get The Message from Message Hashtable and format it with the argumaents passed.
        /// </summary>
        /// <param name="strMessageName">string</param>
        /// <param name="args">param object[]</param>       
        /// <returns>string</returns>
        /// 
        public static string GetMessage(string messageText, params object[] args)
        {
            return (_currentMessage.ContainsKey(messageText) == true ? String.Format(_currentMessage[messageText].ToString(), args) : "");
        }

        ///// <summary>
        ///// Get The Message from Message Hashtable and format it with the argumaents passed.
        ///// </summary>
        ///// <param name="strMessageName">string</param>
        ///// <param name="args">param object[]</param>
        ///// <returns>string</returns>
        //public static string GetMessageValidation(int messageValue, params object[] args)
        //{
        //    return (CurrentMessage.ContainsKey(((ValidationError)messageValue).ToString()) == true ? String.Format(CurrentMessage[((ValidationError)messageValue).ToString()].ToString(), args) : "");
        //}

        #endregion
    }
}
